// Property.cpp: implementation of the Property class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Property.h"
#include "PropertyExpression.h"
#include "PropertyOwner.h"

//#include "supporthanger.h"
//#include "SimpleExpression.h"
#include "PropertyAssembler.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
namespace pa
{
//test no
//	UE_IMPLEMENT_DYNAMIC(Property, Variable);

	Property::Property()
	{
		m_pOwner = NULL;
		//m_strEffectField = "ePART";
		m_bIsCustom = FALSE;
		m_bIsCheckEffect = FALSE;
		m_bIsCheckExist = FALSE;
		m_bIsSave = FALSE;
		m_bIsInitState = FALSE;
	}

	Property::~Property()
	{

	}

	void Property::CopyPropertyData(Property& destinction, Property& source)
	{
		if(source.IsKnown())
			*static_cast<Variable*>(&destinction) = *static_cast<Variable*>(&source);
	
		destinction.Symbol(source.GetSymbol());
		destinction.DataType(source.DataType());
		destinction.m_strDefault = source.m_strDefault;
		destinction.m_strDesc = source.m_strDesc;
		destinction.m_strUnit = source.m_strUnit;
		destinction.m_strEffectField = source.m_strEffectField;
		destinction.m_nPropertyStyle = source.m_nPropertyStyle;
		destinction.m_strEffectCondition = source.m_strEffectCondition;
		destinction.m_strExistCondition = source.m_strExistCondition;
		destinction.m_bIsSave = source.m_bIsSave;
		destinction.m_strSource = source.m_strSource;
	}

	BOOL Property::IsEffect()
	{
		if(GetOwner() == NULL || m_strEffectCondition.IsEmpty()
										|| m_bIsCheckEffect)
		{
			return TRUE;
		}

		m_bIsCheckEffect = TRUE;
		PropertyExpression expr(m_strEffectCondition,
					GetOwner()->GetPropertyContainer());

		Variable var = expr.Caculate();

		m_bIsCheckEffect = FALSE;
		return (BOOL)var;

	}

	BOOL Property::IsExist()
	{
		if(GetOwner() == NULL || m_strExistCondition.IsEmpty()
											|| m_bIsCheckExist)
		{
			return TRUE;
		}

		m_bIsCheckExist = TRUE;
		PropertyExpression expr(m_strExistCondition, 
				GetOwner()->GetPropertyContainer());
		
		Variable var = expr.Caculate();

		m_bIsCheckExist = FALSE;
		return (BOOL)var;
	
	}


	Variable& Property::operator= (const StdString& Value)
	{
		Variable::operator =(Value);
		
		if(GetOwner() != NULL)
		{
			if(GetOwner()->IsReadState())
			{
				GetOwner()->SetModify();
			}
			GetOwner()->RunGetPropertyTrigger();
		}
		return *this;
	}

	Variable& Property::operator= (const Variable& Value)
	{
		(StdString)(*const_cast<Variable*>(&Value));
		Variable::operator =(Value);
		
		if(GetOwner() != NULL)
		{
			if(GetOwner()->IsReadState())
			{
				GetOwner()->SetModify();
			}
			GetOwner()->RunGetPropertyTrigger();
		}
		return *this;
	}
	
	Variable& Property::operator= (const _variant_t& Value)
	{
		Variable::operator= (Value);
		
		if(GetOwner() != NULL)
		{
			if(GetOwner()->IsReadState())
			{
				GetOwner()->SetModify();
			}
			GetOwner()->RunGetPropertyTrigger();
		}
		return *this;
	}

	Variable& Property::operator=(const INT& Value)
	{
		Variable::operator =(Value);
		
		if(GetOwner() != NULL)
		{
			if(GetOwner()->IsReadState())
			{
				GetOwner()->SetModify();
			}
			GetOwner()->RunGetPropertyTrigger();
		}
		return *this;
	}

	Variable& Property::operator= (const DOUBLE& Value)
	{
		Variable::operator =(Value);
		
		if(GetOwner() != NULL)
		{
			if(GetOwner()->IsReadState())
			{
				GetOwner()->SetModify();
			}
			GetOwner()->RunGetPropertyTrigger();
		}
		return *this;
	}

	Variable& Property::operator= (const LONG& Value)
	{
		Variable::operator =(Value);
		
		if(GetOwner() != NULL)
		{
			if(GetOwner()->IsReadState())
			{
				GetOwner()->SetModify();
			}
			GetOwner()->RunGetPropertyTrigger();
		}
		return *this;
	}

	Variable& Property::operator= (const SHORT& Value)
	{
		Variable::operator =(Value);
		
		if(GetOwner() != NULL)
		{
			if(GetOwner()->IsReadState())
			{
				GetOwner()->SetModify();
			}
			GetOwner()->RunGetPropertyTrigger();
		}
		return *this;
	}

	Variable& Property::operator= (const FLOAT& Value)
	{
		Variable::operator =(Value);
		
		if(GetOwner() != NULL)
		{
			if(GetOwner()->IsReadState())
			{
				GetOwner()->SetModify();
			}
			GetOwner()->RunGetPropertyTrigger();
		}
		return *this;
	}

	Variable Property::operator- ()
	{
		Variable::operator -();
		
		if(GetOwner() != NULL)
		{
			if(GetOwner()->IsReadState())
			{
				GetOwner()->SetModify();
			}
			GetOwner()->RunGetPropertyTrigger();
		}
		return *this;
	}

	Property::operator DOUBLE ()
	{
		if(!IsKnown() || !IsEffect())
		{
			InitValue();
		}
		return Variable::operator DOUBLE();
	}

	Property::operator FLOAT()
	{
		if(!IsKnown() || !IsEffect())
		{
			InitValue();
		}
		return Variable::operator FLOAT();
	}

	Property::operator _variant_t()
	{
		if(!IsKnown() || !IsEffect())
		{
			InitValue();
		}
		return Variable::operator _variant_t();
	}

	Property::operator LONG()
	{
		if(!IsKnown() || !IsEffect())
		{
			InitValue();
		}
		return Variable::operator LONG();
	}

	Property::operator INT()
	{
		if(!IsKnown() || !IsEffect())
		{
			InitValue();
		}
		return Variable::operator INT();
	}

	Property::operator SHORT()
	{
		if(!IsKnown() || !IsEffect())
		{
			InitValue();
		}
		return Variable::operator SHORT();
	}

	Property::operator StdString()
	{
		if(!IsKnown() || !IsEffect())
		{
			InitValue();
		}
		return Variable::operator CString();
	}


};